<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统配置 - 管理后台</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark navbar-custom">
        <div class="container-fluid">
            <a class="navbar-brand" href="/admin/index.html">
                <i class="bi bi-shield-check"></i> 管理后台
            </a>
            <div class="ms-auto">
                <span class="navbar-text text-white me-3">
                    <i class="bi bi-person-circle"></i> <span id="adminName"></span>
                </span>
                <a href="#" class="btn btn-outline-light btn-sm" id="logoutBtn">
                    <i class="bi bi-box-arrow-right"></i> 退出
                </a>
            </div>
        </div>
    </nav>

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-2 sidebar">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/index.html">
                            <i class="bi bi-speedometer2"></i> 数据统计
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/seats.html">
                            <i class="bi bi-grid-3x3"></i> 座位管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/bookings.html">
                            <i class="bi bi-calendar-check"></i> 预约管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/violations.html">
                            <i class="bi bi-exclamation-triangle"></i> 违约管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/admin/users.html">
                            <i class="bi bi-people"></i> 用户管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/admin/config.html">
                            <i class="bi bi-gear"></i> 系统配置
                        </a>
                    </li>
                </ul>
            </div>

            <div class="col-md-10 p-4">
                <h4 class="page-header"><i class="bi bi-gear"></i> 系统配置</h4>

                <div class="card">
                    <div class="card-body">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>配置名称</th>
                                    <th>配置值</th>
                                    <th>说明</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody id="configList"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://unpkg.com/layui@2.8.18/dist/layui.js"></script>
    <script src="/js/request.js"></script>
    <script>
        var layer;
        layui.use('layer', function(){
            layer = layui.layer;
        });
    </script>
    <script>
        $(document).ready(function() {
            if (!request.checkLogin()) return;
            const user = request.getUserInfo();
            if (user.role !== 'ADMIN') {
                window.location.href = '/index.html';
                return;
            }
            $('#adminName').text(user.name);
            loadConfigs();
            $('#logoutBtn').click(function(e) {
                e.preventDefault();
                request.logout();
            });
        });

        function loadConfigs() {
            request.get('/api/admin/config/list').then(data => {
                let html = '';
                data.forEach(config => {
                    html += `
                        <tr>
                            <td>${config.configName}</td>
                            <td><span class="badge bg-info">${config.configValue}</span></td>
                            <td><small class="text-muted">${config.configDesc}</small></td>
                            <td>
                                <button class="btn btn-sm btn-primary" onclick="editConfig(${config.id}, '${config.configName}', '${config.configValue}')">修改</button>
                            </td>
                        </tr>
                    `;
                });
                $('#configList').html(html);
            });
        }

        function editConfig(id, name, value) {
            layer.prompt({
                title: '修改配置：' + name,
                formType: 0,
                value: value
            }, function(newValue, index) {
                request.put(`/api/admin/config/${id}`, {value: newValue}).then(() => {
                    layer.close(index);
                    layer.msg('修改成功', {icon: 1});
                    loadConfigs();
                });
            });
        }
    </script>
</body>
</html>

